package com.sdgs.paas.sdk.core.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.codahale.metrics.MetricRegistry;
import com.sdgs.paas.sdk.core.log.PaasLogger;

/**
 * SDK WEBMVC功能自动配置
 *
 * @author wjj
 */
@Configuration
@EnableConfigurationProperties({ PaasHttpProperties.class })
public class PaasWebMVCAutoConfiguration extends WebMvcConfigurerAdapter {
	@Autowired
	MetricRegistry metricRegistry;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(paasHeaderInterceptor());
	}

	@Bean
	public PaasGlobalHandlerExceptionResolver paasGlobalHandlerExceptionResolver() {

		PaasGlobalHandlerExceptionResolver paasGlobalHandlerExceptionResolver = new PaasGlobalHandlerExceptionResolver();

		PaasLogger.info("PaasGlobalHandlerExceptionResolver创建成功!");

		return paasGlobalHandlerExceptionResolver;
	}

	@Bean
	public PaasHttpHeader paasHttpHeader() {

		PaasHttpHeader paasHttpHeader = new PaasHttpHeader();

		PaasLogger.info("PaasHttpHeader创建成功!");

		return paasHttpHeader;
	}

	@Bean
	public PaasHeaderInterceptor paasHeaderInterceptor() {

		PaasHeaderInterceptor paasHeaderInterceptor = new PaasHeaderInterceptor(metricRegistry);

		PaasLogger.info("PaasHeaderInterceptor创建成功!");

		return paasHeaderInterceptor;
	}

}
